package com.monitor.service.parser.impl;

import com.monitor.model.ServerStatus;
import com.monitor.service.parser.MetricParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 系统日志指标解析器
 * 负责解析系统日志信息
 */
public class SystemLogMetricParser implements MetricParser {
    private static final Logger logger = LoggerFactory.getLogger(SystemLogMetricParser.class);

    @Override
    public void parse(String commandOutput, ServerStatus status) {
        try {
            // 只保留前10行日志
            String[] lines = commandOutput.split("\\n");
            StringBuilder sb = new StringBuilder();
            
            int count = Math.min(10, lines.length);
            for (int i = 0; i < count; i++) {
                sb.append(lines[i]).append("\n");
            }
            
            // 使用正确的systemLogs字段保存日志信息
            status.setSystemLogs(sb.toString().trim());
        } catch (Exception e) {
            logger.error("解析系统日志失败: {}", e.getMessage());
            status.setSystemLogs("解析错误");
        }
    }
} 